package lu.med.praxiswahl.wahlumed.client.patienten;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import lu.med.praxiswahl.wahlumed.client.forms.FormsDialog;
import lu.med.praxiswahl.wahlumed.client.forms.FormsPanel;
import lu.med.praxiswahl.wahlumed.client.forms.FormsRefreshListener;
import lu.med.praxiswahl.wahlumed.client.utilities.ColumnWidthControler;
import lu.med.praxiswahl.wahlumed.client.utilities.UniCode;
import lu.med.praxiswahl.wahlumed.client.wclient.Main;
import lu.med.praxiswahl.wahlumed.client.wclient.PatientenSuchmaske;
import lu.med.praxiswahl.wahlumed.client.wclient.PatientenSuchmaskeListener;
import lu.med.praxiswahl.wahlumed.model.beans.FormularTBL;

@SuppressWarnings("serial")
public class MutterschutzDPanel extends JPanel implements ActionListener,
	PatientenSuchmaskeListener, ListSelectionListener, FormsRefreshListener
{

    private String matricule = "";
    private Main dataManager;
    private DefaultListModel listModel;
    private JList list;
    private FormularTBL[] forms;
    private int lastIndex = -1;

    JTabbedPane tabbedPane;

    public MutterschutzDPanel(Main dataManager, PatientenSuchmaske pm,
	    JTabbedPane tabbedPane)
    {
	super(new BorderLayout());
	this.tabbedPane = tabbedPane;
	this.dataManager = dataManager;
	pm.addSuchmaskeListener(this);

	JPanel listPanel, buttonPanel;

	listPanel = new JPanel(new BorderLayout());
	listModel = new DefaultListModel();
	list = new JList(listModel);
	list.addListSelectionListener(this);
	listPanel.add(list, BorderLayout.CENTER);
	listPanel.setBorder(BorderFactory.createTitledBorder("Datens"
		+ UniCode.getae() + "tze"));
	ColumnWidthControler cwc = new ColumnWidthControler(
		Main.WIDTH_CONFIGURATION_FILE, "mutterschutzD");
	list.setPreferredSize(new Dimension(cwc.getColumnWidth(0), 0));
	this.add(listPanel, BorderLayout.WEST);

	buttonPanel = new JPanel(new BorderLayout());
	JButton newButton = new JButton("Neues Formular anlegen");
	newButton.addActionListener(this);
	buttonPanel.add(newButton, BorderLayout.NORTH);
	this.add(buttonPanel, BorderLayout.CENTER);
    }

    public void actionPerformed(ActionEvent e)
    {
	if (e.getActionCommand().equals("Neues Formular anlegen"))
	{
	    if (matricule.equals(""))
	    {
		JOptionPane.showMessageDialog(dataManager, "Sie m"
			+ UniCode.getue() + "ssen zuerst eine Patientin ausw"
			+ UniCode.getae() + "hlen");
		return;
		// Öffnet den Dialog mit neuer Datei
	    }
	    new FormsDialog(dataManager, matricule,
		    FormsPanel.FORM_TYPE_MUTTERSCHUTZ_D, "", this, -1l, 1);
	}

    }

    public void modelChanged(String matricule)
    {
	/*
	 * this.matricule = matricule; lastIndex = -1; refresh();
	 */

	if (this.tabbedPane.getSelectedIndex() == 10)
	{
	    this.matricule = matricule;
	    lastIndex = -1;
	    refresh();
	}
    }

    public void refresh()
    {
	// Liste die Liste vorhandener Datensätze aus
	if (matricule.equals(""))
	{
	    return; // Speichert die erhaltenen TBL Objekte und stellt daraus
	    // die Liste zusammen
	}
	listModel.clear();

	forms = dataManager.getFormContext().getFormular_BYCustomer_and_BYtyp(
		matricule, "" + FormsPanel.FORM_TYPE_MUTTERSCHUTZ_D);

	// Überträgt die Einträge in das Model
	for (int i = 0; i < forms.length; i++)
	{
	    int d, m, y;
	    String da = "";
	    Calendar c = forms[i].getDatum();
	    d = c.get(Calendar.DAY_OF_MONTH);
	    m = c.get(Calendar.MONTH) + 1;
	    y = c.get(Calendar.YEAR);
	    if (d < 10)
	    {
		da += "0" + d + ".";
	    }
	    else
	    {
		da += d + ".";
	    }
	    if (m < 10)
	    {
		da += "0" + m + ".";
	    }
	    else
	    {
		da += m + ".";
	    }
	    da += y;
	    listModel.add(i, "" + da + "   " + forms[i].getBezeichner());
	}
    }

    public void valueChanged(ListSelectionEvent e)
    {
	// Die entsprechenden Daten werden geladen
	if (list.getSelectedIndex() == -1)
	{
	    return;
	}
	int i = list.getSelectedIndex();

	if (i == lastIndex)
	{
	    return;
	}
	lastIndex = i;

	// Öffnet den Dialog mit der entsprechenden Datei
	new FormsDialog(dataManager, matricule,
		FormsPanel.FORM_TYPE_MUTTERSCHUTZ_D, forms[i].getSpeicherOrt(),
		this, forms[i].getId(), 1);
    }

    /**
     * @return the matricule
     */
    public String getMatricule()
    {
	return matricule;
    }

    /**
     * @param matricule
     *            the matricule to set
     */
    public void setMatricule(String matricule)
    {
	this.matricule = matricule;
    }
}
